import request from '@/utils/request'
import type {
  Role,
  RoleQuery,
  RoleList,
  CreateRole,
  UpdateRole,
  RolePermissionData
} from '@/types/api/system/role'
import type { API } from '@/types/api/response'

/**
 * 获取角色列表
 */
export const getRoleList = (params: RoleQuery) => {
  return request.get<API.Response<RoleList>>('/api/system/role/list', { params })
}

/**
 * 获取角色详情
 */
export const getRoleDetail = (id: string) => {
  return request.get<API.Response<Role>>(`/api/system/role/${id}`)
}

/**
 * 创建角色
 */
export const createRole = (data: CreateRole) => {
  return request.post<API.Response<Role>>('/api/system/role', data)
}

/**
 * 更新角色
 */
export const updateRole = (id: string, data: UpdateRole) => {
  return request.put<API.Response<Role>>(`/api/system/role/${id}`, data)
}

/**
 * 批量删除角色
 * @param ids 角色ID数组
 */
export const deleteRole = (ids: string[]) => {
  return request.delete<API.Response<null>>('/api/system/role', { data: { ids } })
}

/**
 * 修改角色状态
 * @param id 角色ID
 * @param status 状态(0:禁用 1:启用)
 */
export const changeRoleStatus = (id: string, status: number) => {
  return request.put<API.Response<null>>(`/api/system/role/status/${id}`, { status })
}

/**
 * 获取角色权限
 */
export const getRolePermissions = (id: string) => {
  return request.get<API.Response<RolePermissionData>>(`/api/system/role/permissions/${id}`)
}

/**
 * 保存角色权限
 */
export const saveRolePermissions = (id: string, permissions: string[]) => {
  if (!id || id === 'undefined') {
    return Promise.reject(new Error('角色ID无效'))
  }
  return request.put<API.Response<null>>(`/api/system/role/permissions/${id}`, { permissions })
} 